package org.cmisarium.xcmis.fsprovider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xcmis.spi.ChangeTokenHolder;
import org.xcmis.spi.CmisConstants;
import org.xcmis.spi.CmisRegistry;
import org.xcmis.spi.Connection;
import org.xcmis.spi.ConstraintException;
import org.xcmis.spi.ContentStream;
import org.xcmis.spi.FilterNotValidException;
import org.xcmis.spi.ItemsList;
import org.xcmis.spi.NameConstraintViolationException;
import org.xcmis.spi.ObjectNotFoundException;
import org.xcmis.spi.StorageException;
import org.xcmis.spi.StorageProvider;
import org.xcmis.spi.StreamNotSupportedException;
import org.xcmis.spi.TypeNotFoundException;
import org.xcmis.spi.UpdateConflictException;
import org.xcmis.spi.VersioningException;
import org.xcmis.spi.model.AccessControlEntry;
import org.xcmis.spi.model.CmisObject;
import org.xcmis.spi.model.IncludeRelationships;
import org.xcmis.spi.model.Property;
import org.xcmis.spi.model.VersioningState;
import org.xcmis.spi.model.impl.IdProperty;
import org.xcmis.spi.model.impl.StringProperty;

public class CmisableFsMain
{

   /**
    * @param args
    */
   public static void main(String[] args) throws IOException, StreamNotSupportedException, ConstraintException,
      FilterNotValidException, ObjectNotFoundException, StorageException, NameConstraintViolationException,
      TypeNotFoundException, VersioningException, UpdateConflictException
   {

      CmisRegistry registry = CmisRegistry.getInstance();

      StorageProvider cfProvider =
         new StorageProviderImpl("/Users/gennadyazarenkov/Documents/cmistest", "testId", "testName");

      registry.addStorage(cfProvider);

      Connection conn = registry.getConnection("testId");

      String rootId = conn.getStorage().getRepositoryInfo().getRootFolderId();

      System.out.println(rootId);

      // id and path are the same
      String path = "myfile.txt";

      CmisObject obj = conn.getObjectByPath(path, false, null, false, false, true, null, null);

      String id = obj.getObjectInfo().getId();

      System.out.println(obj.getObjectInfo().getTypeId());

      System.out.println("file name:" + obj.getProperties().get("cmis:name").getValues().get(0));

      System.out.println("file id:" + obj.getProperties().get("cmis:objectId").getValues().get(0));

      System.out.println("file base type :" + obj.getProperties().get("cmis:baseTypeId").getValues().get(0));

      ContentStream stream = conn.getContentStream(id, null);

      System.out.println(stream.getMediaType());

      System.out.println(stream.getStream().available());

      ItemsList<CmisObject> list =
         conn.getChildren(PathCoder.pathToId("folder1"), false, IncludeRelationships.NONE, false, true, null, null,
            null, -1, 0);

      System.out.println(list.getNumItems());
      System.out.println(list.getItems().get(1).getProperties().get("cmis:objectId").getValues().get(0));

      CmisObject obj1 = conn.getObjectByPath("folder1/myfile.txt", false, null, false, false, true, null, null);
      System.out.println(obj1.getObjectInfo().getId());

      Map<String, Property<?>> properties = new HashMap<String, Property<?>>();
      List<String> values = new ArrayList<String>();
      values.add(CmisConstants.FOLDER);

      properties.put(CmisConstants.OBJECT_TYPE_ID, new IdProperty(CmisConstants.OBJECT_TYPE_ID,
         CmisConstants.OBJECT_TYPE_ID, CmisConstants.OBJECT_TYPE_ID, CmisConstants.OBJECT_TYPE_ID, values));
      properties.put(CmisConstants.NAME, new StringProperty(CmisConstants.NAME, CmisConstants.NAME, CmisConstants.NAME,
         CmisConstants.NAME, "my"));

      String id1 =
         conn.createFolder(rootId, properties, new ArrayList<AccessControlEntry>(),
            new ArrayList<AccessControlEntry>(), new ArrayList<String>());

      System.out.println(id1 + " " + PathCoder.idToPath(id1));

      conn.deleteObject(id1, true);

      Map<String, Property<?>> properties2 = new HashMap<String, Property<?>>();
      List<String> values2 = new ArrayList<String>();
      values2.add(CmisConstants.DOCUMENT);
      properties2.put(CmisConstants.OBJECT_TYPE_ID, new IdProperty(CmisConstants.OBJECT_TYPE_ID,
         CmisConstants.OBJECT_TYPE_ID, CmisConstants.OBJECT_TYPE_ID, CmisConstants.OBJECT_TYPE_ID, values2));
      properties2.put(CmisConstants.NAME, new StringProperty(CmisConstants.NAME, CmisConstants.NAME,
         CmisConstants.NAME, CmisConstants.NAME, "my-file"));

      String id2 =
         conn.createDocument(rootId, properties2, null, new ArrayList<AccessControlEntry>(),
            new ArrayList<AccessControlEntry>(), new ArrayList<String>(), VersioningState.NONE);

      System.out.println(id2 + " " + PathCoder.idToPath(id2));

      Map<String, Property<?>> properties3 = new HashMap<String, Property<?>>();
      List<String> values3 = new ArrayList<String>();
      values3.add(CmisConstants.DOCUMENT);

      properties3.put(CmisConstants.NAME, new StringProperty(CmisConstants.NAME, CmisConstants.NAME,
         CmisConstants.NAME, CmisConstants.NAME, "my-file222"));
      //ChangeTokenHolder holder = new ChangeTokenHolder();
      String id3 = conn.updateProperties(id2, new ChangeTokenHolder(), properties3);

      System.out.println(id3 + " " + PathCoder.idToPath(id3));

      conn.deleteObject(id3, true);

   }

}
